import * as WebSocket from 'ws';
import { LoginManager } from '../LoginManager';

export class ClientSocket
{
    private _ws:WebSocket;
    constructor()
    {
        
    }

    private open():void
    {
        // this._ws.send('something');
        console.log('websocket connect success!!');
        // let login = new LoginProto();
        // login.body.acc = '111';
        // this.send(login.getStr());
    }

    private incoming(data:any):void
    {
        console.log(data);
    }

    public init(url:string):void
    {
        this._ws = new WebSocket('ws:127.0.0.1:3000');
        let self = this;
        this._ws.on('open',function()
        {
            self.open();
        });
        this._ws.on('message',function(data){
            // self.incoming(data);
            LoginManager.getInstance().messageParse(data);
        });
        this._ws.on('error',function(err){
            console.warn('socket error',err);
        });
        this._ws.on('close',function(code,msg){
            console.warn('socket close:',code,msg);
        });
    }

    public send(data):void
    {
        this._ws.send(data);
    }
}